
package msu.ml.gis;

public class NumericConverter implements IDBFAttributeConverter
{
   private int length;
   public NumericConverter(int length)
   {
      this.length = length;
   }

   public byte [] convertValue(Object o)
   {
      try
      {
         if(o instanceof Integer)
         {
            Integer i = ((Integer)o);
            String form = "%1$" + this.length + "d";
            String result = String.format(form, i.intValue());
            return result.getBytes("UTF-8");
         }
         else if(o instanceof Double)
         {
            Double d = ((Double)o);
            String form = "%1$" + this.length + ".2f";
            String result = String.format(form, d.doubleValue());
            return result.getBytes("UTF-8");
         }
         else if(o instanceof Float)
         {
            Float f = ((Float)o);
            String form = "%1$" + this.length + ".2f";
            String result = String.format(form, f.floatValue());
            return result.getBytes("UTF-8");
         }
      }
      catch(java.io.UnsupportedEncodingException e)
      {
         System.out.println("Unsupported Encoding");
         return null;
      }

      return null;

   }
}


